﻿using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VisionApp.Interface
{
    public interface ILogger
    {
        int Imformation(string info);
        int Error(string error);
        int Data(string name, string data);
    }

    public class Logger : ILogger
    {
        private ImgInfo _imInfo;
        private ICfgIO _cfgIO;

        public Logger()
        {
            _cfgIO = new CfgIO();
            _cfgIO.Load("ImgInfo", ref _imInfo);
            if (_imInfo == null)
            {
                _imInfo = new ImgInfo();
                _cfgIO.Save("ImgInfo", _imInfo);
            }
        }

        public int Data(string name, string data)
        {
            writeLog(name, "data", data);
            return 0;
        }

        public int Error(string error)
        {
            try
            {
                Messenger.Default.Send<string>(error);
                writeLog("Log", "Error", error);
                return 0;
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.Write("error in write logs");
                return -1;
            }
        }

        public int Error(string error, Exception e)
        {
            try
            {
                Messenger.Default.Send<string>(error);
                writeLog("Log", "Error", error+ e.ToString());
                return 0;
            }
            catch (Exception e1)
            {
                System.Diagnostics.Debug.Write("error in write logs");
                return -1;
            }
        }

        public void IncImgID()
        {
            _imInfo.ID++;
            _cfgIO.Save<ImgInfo>("ImgInfo", _imInfo);
        }
        public int Imformation(string info)
        {
            try
            {
                Messenger.Default.Send<string>(info);
                //writeLog("Log", "Info", info);
                return 0;
            }
            catch (Exception e)
            {
                return -1;
            }
        }

        private void writeLog(string name, string type, string msg)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            try
            {
                if (!string.IsNullOrEmpty(path))
                {
                    path = AppDomain.CurrentDomain.BaseDirectory + name;
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + "\\" + DateTime.Now.ToString("yyyyMMdd");
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                    if (!File.Exists(path))
                    {
                        FileStream fs = File.Create(path);
                        fs.Close();
                    }
                    if (File.Exists(path))
                    {
                        StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default);
                        sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + type + "-->" + msg);
                        sw.WriteLine("----------------------------------------");
                        sw.Close();
                    }
                }
            }
            catch (Exception e)
            {
                return;
            }

        }
    }

    public class ImgInfo
    {
        public int ID;
    }
}
